﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesLib;

namespace Deloitte.iTunes
{
    public class iTunesUtility
    {        
        public static iTunesApp CreateITunesApp()
        {
            iTunesApp app = new iTunesAppClass();
            return app;
        }
        public static IITSourceCollection GetSourceCollection(iTunesApp app)
        {
            return app.Sources;
        }
        public static IITPlaylistCollection GetPlayLists(IITSource source)
        {
            return source.Playlists;
        }
        public static List<string> GetSourceArtists(IITSource source)
        {
            List<string> artists = new List<string>();
            List<string> playListArtists = new List<string>();
            foreach (IITPlaylist playlist in source.Playlists)
            {
                playListArtists = GetPlaylistArtists(playlist);
                foreach (string artist in playListArtists)
                {
                    AddArtistToList(artists, artist);
                }
            }
            return artists;
        }
        private static void AddArtistToList(List<string> list, string artist)
        {
            if (artist != null)
            {
                if (!list.Contains(artist)) list.Add(artist);
            }
        }
        public static List<string> GetPlaylistArtists(IITPlaylist playlist)
        {
            List<string> artists = new List<string>();
            foreach (IITTrack track in playlist.Tracks)
            {
                AddArtistToList(artists, track.Artist);
            }
            return artists;
        }
        public static List<string> GetAllArtists()
        {
            iTunesApp app = CreateITunesApp();
            IITSourceCollection sources = app.Sources;

            List<string> artists = new List<string>();
            List<string> sourceArtists = new List<string>();
            foreach (IITSource source in app.Sources)
            {
                sourceArtists = GetSourceArtists(source);
                foreach (string artist in sourceArtists)
                {
                    AddArtistToList(artists, artist);
                }
            }
            return artists;
        }
    }
}

//    iTunesApp app = new iTunesAppClass();
//        IITSourceCollection sources = app.Sources;
//        this.labelStatus.Text = "> Finding sources.";
//        foreach (IITSource source in sources)
//        {
//            if ((source.Kind == ITSourceKind.ITSourceKindLibrary) || (source.Kind == ITSourceKind.ITSourceKindIPod))
//            {
//                this.labelStatus.Text = "> Source found.";
//                builder.Append("Kind: " + source.Kind + Environment.NewLine);
//                builder.Append("Name: " + source.Name + Environment.NewLine);
//                builder.Append("Capacity: " + source.Capacity + Environment.NewLine);
//                builder.Append("Free space: " + source.FreeSpace + Environment.NewLine);
//                foreach (IITPlaylist playlist in source.Playlists)
//                {
//                    this.labelStatus.Text = "> Playlist found.";
//                    builder.Append("Playlist Name: " + playlist.Name + Environment.NewLine);
//                    builder.Append("Kind: " + playlist.Kind + Environment.NewLine);
//                    this.labelStatus.Text = "> There are " + playlist.Tracks.Count.ToString() + " tracks in the playlist.";
//                    num = 0;
//                    foreach (IITTrack track in playlist.Tracks)
//                    {
//                        if (playlist.Name == "Music")
//                        {
//                            builder.Append("\t" + track.Name + "\t");
//                            if (track.Rating <= 0)
//                            {
//                                builder.Append("0 stars");
//                            }
//                            else
//                            {
//                                builder.Append((track.Rating / 20) + " stars");
//                            }
//                            builder.Append(Environment.NewLine);
//                            num++;
//                        }
//                        if (num >= 10)
//                        {
//                            this.labelStatus.Text = "> Maximum number of songs returned.";
//                            break;
//                        }
//                    }
//                }
//                builder.Append(Environment.NewLine);
//            }
//        }
//        this.labelStatus.Text = "> Outputting data.";
//        this.textResults.Text = builder.ToString();
//        this.labelStatus.Text = "> Output complete.";
//    }
//    catch (Exception exception)
//    {
//        this.textResults.Text = this.textResults.Text + "Application exception. Message: " + exception.Message;
//    }
//    builder = null;
//    num = 0;
//}

 

 
